﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface.Documents;
using System.Windows.Documents;
using System.IO;
using System.Windows;
using ICSharpCode.AvalonEdit.Document;
using Jbe.TestSuite.CodeEditor.Properties;

namespace Jbe.TestSuite.CodeEditor.Domain
{
    public class CodeDocumentType : DocumentType
    {
        public int documentCount;


        public CodeDocumentType()
            : base(Resources.CodeDocuments, "rb")
        {
        }

        public override bool CanNew() { return true; }

        protected override Document NewCore()
        {
            TextDocument content = new TextDocument();
            CodeDocument document = new CodeDocument(content);
            document.FileName = string.Format(Resources.DocumentTemplate + ".rb", ++documentCount);
            return document;
        }

        public override bool CanOpen() { return true; }

        protected override Document OpenCore(string fileName)
        {
            TextDocument content = new TextDocument();
            using (StreamReader sr = new StreamReader(fileName))
            {
                content.Text = sr.ReadToEnd();
            }

            CodeDocument document = new CodeDocument(content);
            documentCount++;
            return document;
        }

        public override bool CanSave(Document document) { return document is CodeDocument; }

        protected override void SaveCore(Document document, string fileName)
        {
            TextDocument content = ((CodeDocument)document).Content;
            using (StreamWriter writer = new StreamWriter(fileName))
            {
                writer.Write(content.Text);
            }
        }
    }
}
